﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
//using PopupControl;
//using _180FrameWork.Common;
using System.Windows.Forms;

namespace _180FrameWork.CustControls
{
    public partial class CustFuzzyQueryComboBox : ComboBox
    {
        public CustFuzzyQueryComboBox()
        {
            InitializeComponent();

            Init();
        }

        public CustFuzzyQueryComboBox(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            Init();
        }

        private void Init()
        {
            DelayTimer.Elapsed += new System.Timers.ElapsedEventHandler(DelayTimer_Elapsed);
        }

        private bool _needUpdate = false;

        private System.Timers.Timer DelayTimer = new System.Timers.Timer(300);

        #region
        ///// <summary>
        ///// 查询id
        ///// </summary>
        //[DefaultValue("123")]
        //public string QueryId { get { return this._QueryId; } set { this._QueryId = value; } }
        //private string _QueryId = "123";
        #endregion

        private void CustFuzzyQueryComboBox_TextChanged(object sender, EventArgs e)
        {
            _needUpdate = true;
            DelayTimer.Stop();
            DelayTimer.Start();
        }

        private void UpdateData()
        {
            if (_needUpdate)
            {
                var q = this.Text;
                if (QueryStringChangeEvent != null)
                    QueryStringChangeEvent(q);
                /*if (q.Length > 0)
                {
                    //获取远程数据
                    WebRequestHelper wh = new WebRequestHelper();
                    IDictionary<string, string> dict = new Dictionary<string, string>();
                    dict.Add("QueryId", QueryId);
                    dict.Add("q", q);
                    dict.Add("PageSize", "20");
                    string strJson = wh.DoGet("http://10.180.180.100:1983/api/easyQueryService/FuzzyQuery", dict);
                    IList<dynamic> data = strJson.JsonToObj<IList<dynamic>>();

                    var text = this.Text;
                    if (data.Count > 0)
                    {
                        this.TextChanged -= new EventHandler(CustFuzzyQueryComboBox_TextChanged);
                        this.DataSource = data;
                        this.DroppedDown = true;
                        this.Text = q;
                        this.SelectionStart = q.Length;
                        this.TextChanged += new EventHandler(CustFuzzyQueryComboBox_TextChanged);
                    }
                    else
                    {
                        this.DroppedDown = false;
                        //this.SelectionStart = text.Length;
                    }
                }*/
            }
        }

        private void CustFuzzyQueryComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            _needUpdate = false;
        }

        private void CustFuzzyQueryComboBox_TextUpdate(object sender, EventArgs e)
        {
            _needUpdate = false;
        }

        void DelayTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DelayTimer.Stop();
            this.Invoke(new DelayOverHandler(UpdateData), null);
        }

        public delegate void DelayOverHandler();
        /// <summary>
        /// 查询字符串变更事件
        /// </summary>
        public event Action<string> QueryStringChangeEvent;

        public new object DataSource
        {
            get
            {
                return base.DataSource;
            }
            set
            {
                var q = this.Text;
                this.TextChanged -= new EventHandler(CustFuzzyQueryComboBox_TextChanged);
                base.DataSource = value;
                this.DroppedDown = true;
                this.Text = q;
                this.SelectionStart = q.Length;
                this.TextChanged += new EventHandler(CustFuzzyQueryComboBox_TextChanged);
            }
        }
    }
}
